package com.naihe.anderson.sms.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.listener.OnRecordListener;
import com.naihe.anderson.sms.manager.RecordMoneyMgr;

public class MoneyActivity extends Activity implements OnClickListener,OnItemClickListener ,OnRecordListener{

	Button back_btn;
	GridView gridView;
	
	TextView today_income_et;
	TextView today_expenditure_et;
	TextView month_income_et;
	TextView month_expenditure_et;
	
	NhSmsApp nhSmsApp;
	RecordMoneyMgr moneyMgr;
	
	public static final int MSG_WHAT_DATECHANGE_ACTIVITY = 0;
	public static final int MSG_WHAT_CALCULATE = 1;
	Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_WHAT_DATECHANGE_ACTIVITY:
				today_income_et.setText("￥" + moneyMgr.getToday_income());
				today_expenditure_et.setText("￥" + moneyMgr.getToday_expenditure());
				month_income_et.setText("￥" + moneyMgr.getMonth_income());
				month_expenditure_et.setText("￥" + moneyMgr.getMonth_expenditure());
				break;
			case MSG_WHAT_CALCULATE:
				moneyMgr.calculateRecordMoney();
				break;
			default:
				break;
			}
		}
	};
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.money_layout);
        nhSmsApp = (NhSmsApp) getApplication();
        nhSmsApp.setActivity(this);
        moneyMgr = nhSmsApp.getRecordMoneyMgr();
        moneyMgr.setOnMoneyListener(this);
        
        mHandler.sendEmptyMessage(MSG_WHAT_CALCULATE);
        initView();
    }
	
	private void toFinishActivity(){
		nhSmsApp.setActivity(null);
		moneyMgr.setOnMoneyListener(null);
		MoneyActivity.this.finish();
		System.gc();
	}
	
	public void initView(){
		today_income_et = (TextView) findViewById(R.id.today_income_et);
		today_income_et.setText("￥" + moneyMgr.getToday_income());
		today_expenditure_et = (TextView) findViewById(R.id.today_expenditure_et);
		today_expenditure_et.setText("￥" + moneyMgr.getToday_expenditure());
		month_income_et = (TextView) findViewById(R.id.month_income_et);
		month_income_et.setText("￥" + moneyMgr.getMonth_income());
		month_expenditure_et = (TextView) findViewById(R.id.month_expenditure_et);
		month_expenditure_et.setText("￥" + moneyMgr.getMonth_expenditure());
		
		back_btn = (Button) findViewById(R.id.back_btn);
		back_btn.setOnClickListener(this);
		
		gridView = (GridView) findViewById(R.id.gridView);
	    gridView.setAdapter(new ImageAdapter(MoneyActivity.this));
	    gridView.setOnItemClickListener(this);
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Integer mTitleId = ((ImageAdapter)parent.getAdapter()).getItem(position);
		if(mTitleId != null){
			switch (mTitleId.intValue()) {
			case R.string.money_001:
				Intent ExpenditureIntent = new Intent(MoneyActivity.this, NewExpenditureActivity.class);
				MoneyActivity.this.startActivity(ExpenditureIntent);
				break;
			case R.string.money_002:
				Intent IncomeIntent = new Intent(MoneyActivity.this, NewIncomeActivity.class);
				MoneyActivity.this.startActivity(IncomeIntent);
				break;
			case R.string.money_004:
				Intent RecordMainIntent = new Intent(MoneyActivity.this, RecordMainActivity.class);
				MoneyActivity.this.startActivity(RecordMainIntent);
				break;
			case R.string.money_003:
				Intent CapitalIntent = new Intent(MoneyActivity.this, CapitalNumberInfoActivity.class);
				MoneyActivity.this.startActivity(CapitalIntent);
				break;
			case R.string.money_006:
				Intent setIntent = new Intent(MoneyActivity.this, MoneySetActivity.class);
				MoneyActivity.this.startActivity(setIntent);
				break;
			default:
				break;
			}
		}
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.back_btn){
			toFinishActivity();
		} else {
			
		}
	}
    
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 截获按键事件
        if(KeyEvent.KEYCODE_BACK == keyCode){
        	toFinishActivity();
        	return true;
		}

        return super.onKeyDown(keyCode, event);
    }
	
	public class ImageAdapter extends BaseAdapter {

		Context myContext;
		
	    private Integer[] mImageIds = {
	    		R.drawable.money_001,
	    		R.drawable.money_002,
	    		R.drawable.money_003,
	    		R.drawable.money_004,
	    		R.drawable.money_005,
	    		R.drawable.money_006
	    };

	    private Integer[] mTitleIds = { 
	    		R.string.money_001,
	    		R.string.money_002,
	    		R.string.money_003,
	    		R.string.money_004,
	    		R.string.money_005,
	    		R.string.money_006 
	    };

	    public ImageAdapter(Context context) {
	    	myContext = context;
	    }

	    @Override
	    public int getCount() {
	        return mTitleIds.length;

	    }

	    @Override
	    public Integer getItem(int position) {
	    	if(position > mTitleIds.length)
	    		return null;
	        return mTitleIds[position];

	    }

	    @Override
	    public long getItemId(int position) {
	        return position;

	    }

	    @Override
	    public View getView(int position, View convertView, ViewGroup parent) {
	        View view;
	        if (convertView == null) {
	            LayoutInflater inflater = LayoutInflater.from(myContext);
	            view = inflater.inflate(R.layout.money_grid_view_item, null);
	        } else {
	            view = convertView;
	        }

	        ImageView image = (ImageView) view.findViewById(R.id.grItemImage);
	        image.setImageResource(mImageIds[position]);
	        TextView grTextTitle = (TextView) view.findViewById(R.id.grTextTitle);
	        grTextTitle.setText(mTitleIds[position]);
	        return view;

	    }
	}

	@Override
	public void onDataChanged() {
		mHandler.sendEmptyMessage(MSG_WHAT_DATECHANGE_ACTIVITY);
	}

}
